package handler

import (
	redigo "github.com/gomodule/redigo/redis"
	"meinong/hardware/global"
	"time"
	"fmt"
)

//RFID 锁
func GetNowRFID(KeyTable RedisKey) {
	redisClient := global.GlobalRedi.Get()
	defer redisClient.Close()
	for {
		//当前rfid值
		node, err := redigo.String(redisClient.Do("rpop", KeyTable.QueueName))
		if err != nil {
			if err == redigo.ErrNil {
				fmt.Println("rfidList nil")
				continue
			} else {
				panic(err)
			}
		}

		if node != "" {

			lock_name, err := redisClient.Do("Get", KeyTable.LockName)
			if err != nil {
				fmt.Println(err.Error())
			}

			if lock_name == nil {
				//5秒过期
				_, err = redisClient.Do("Set", KeyTable.LockName, node, "EX", 5)
				if err != nil {
					panic(err)
				}
			}
			if fmt.Sprintf("%s", lock_name) == node {
				//5秒过期
				_, err = redisClient.Do("EXPIRE", KeyTable.LockName, 5)
				if err != nil {
					panic(err)
				}
			}
		} else {
			fmt.Println("当前队列空")
		}
		time.Sleep(time.Millisecond * 100)
	}
}